package ac.zlf.springmybatis.core.mail;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.mail.MailException;
import org.springframework.mail.MailParseException;
import org.springframework.mail.MailPreparationException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

@Component
public class MailServiceImpl
        implements MailService {
    private static final Logger logger = Logger.getLogger(MailServiceImpl.class);
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private Configuration freemarkerConfiguration;
    @Autowired
    private TaskExecutor taskExecutor;
    @Autowired
    private String encoding;
    @Autowired
    private InternetAddress fromAddress;

    public void sendSimpleMail(MailMessageVO mailMessage)
            throws MailException {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(this.fromAddress.getAddress());
        message.setTo(mailMessage.getTo());
        message.setCc(mailMessage.getCc());
        message.setBcc(mailMessage.getBcc());
        message.setSubject(mailMessage.getSubject());
        message.setText(mailMessage.getContent());
        message.setSentDate(new Date());
        this.javaMailSender.send(message);
    }

    public void sendHTMLMail(MailMessageVO mailMessage, String htmlCnt)
            throws MailException {
        MimeMessage mimeMessage = this.javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = null;
        try {
            if (mailMessage.getAttachmentFiles() != null) {
                mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, this.encoding);
                for (File attachmentFile : mailMessage.getAttachmentFiles()) {
                    mimeMessageHelper.addAttachment(attachmentFile.getName(), attachmentFile);
                }
            } else {
                mimeMessageHelper = new MimeMessageHelper(mimeMessage, false, this.encoding);
            }
            mimeMessageHelper.setFrom(this.fromAddress);
            mimeMessageHelper.setTo(mailMessage.getTo());
            if (mailMessage.getBcc() != null) {
                mimeMessageHelper.setBcc(mailMessage.getBcc());
            }
            if (mailMessage.getCc() != null) {
                mimeMessageHelper.setCc(mailMessage.getCc());
            }
            mimeMessageHelper.setSubject(mailMessage.getSubject());
            mimeMessageHelper.setSentDate(new Date());
            mimeMessageHelper.setText(htmlCnt, true);
        } catch (MessagingException ex) {
            logger.error(ex.getMessage());
            throw new MailPreparationException(ex.getMessage(), ex);
        }
        this.javaMailSender.send(mimeMessage);
    }

    public void setTemplateMail(MailMessageVO mailMessage, String template, Map<String, Object> parse)
            throws MailException {
        try {
            Template templateS = this.freemarkerConfiguration.getTemplate(template, this.encoding);
            String htmlCnt = FreeMarkerTemplateUtils.processTemplateIntoString(templateS, parse);
            sendHTMLMail(mailMessage, htmlCnt);
        } catch (IOException ex) {
            logger.error(ex.getMessage());
            throw new MailParseException(ex.getMessage(), ex);
        } catch (TemplateException ex) {
            logger.error(ex.getMessage());
            throw new MailParseException(ex.getMessage(), ex);
        } catch (MailException ex) {
            logger.error(ex.getMessage());
            throw ex;
        }
    }

    public void setAsyncSimpleMail(final MailMessageVO mailMessage) {
        this.taskExecutor.execute(new Runnable() {
            public void run() {
                try {
                    MailServiceImpl.this.sendSimpleMail(mailMessage);
                } catch (Exception e) {
                    MailServiceImpl.logger.error("Send Mail Failure: " + e.getMessage());
                    e.printStackTrace();
                }
            }
        });
    }

    public void setAsyncTemplateMail(final MailMessageVO mailMessage, final String template, final Map<String, Object> parse) {
        this.taskExecutor.execute(new Runnable() {
            public void run() {
                try {
                    MailServiceImpl.this.setTemplateMail(mailMessage, template, parse);
                } catch (MailException e) {
                    MailServiceImpl.logger.error("Send Mail Failure: " + e.getMessage());
                    e.printStackTrace();
                }
            }
        });
    }
}



/* Location:           D:\repository\cc\hwtech\core\2.0.1-school-SNAPSHOT\core-2.0.1-school-SNAPSHOT.jar

 * Qualified Name:     MailServiceImpl

 * JD-Core Version:    0.7.0.1

 */
